package cn.edu.zut.internship.service.impl.sys;


import cn.edu.zut.internship.entity.AuthSysMenu;
import cn.edu.zut.internship.entity.AuthSysMenuMeta;
import cn.edu.zut.internship.iservice.sys.IMenuService;
import cn.edu.zut.internship.mapper.sys.AuthSysMenuMapper;
import cn.edu.zut.internship.mapper.sys.AuthSysMenuMetaMapper;
import cn.edu.zut.internship.utils.PrimaryKeyUtil;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;

import java.util.ArrayList;
import java.util.List;

@Service
public class MenuServiceImpl implements IMenuService {
    @Autowired
    private AuthSysMenuMapper menuMapper;
    @Autowired
    private AuthSysMenuMetaMapper metaMapper;
    @Autowired
    private AuthSysMenuMetaMapper authSysMenuMetaMapper;
    @Autowired
    private PrimaryKeyUtil primaryKeyUtil;
    @Override
    public List<AuthSysMenu> findAllMenus() {
        //查询所有一级菜单
        List<AuthSysMenu> authSysMenus = menuMapper.selectFistMenu();
        //查询子菜单
        for(AuthSysMenu authSysMenu:authSysMenus){
            //查询路由的模板
            AuthSysMenuMeta authSysMenuMeta = authSysMenuMetaMapper.selectByMnId(authSysMenu.getId());
            authSysMenu.setMeta(authSysMenuMeta);
            //查询子路由
            authSysMenu.setChildren(getChildrenMenus(authSysMenu.getId()));
            //查询路由的角色信息
            List<Long> muneRoleList = menuMapper.getMuneRoleList(authSysMenu.getId());
            authSysMenu.setRoles(muneRoleList);
        }
        return authSysMenus;
    }

    @Override
    public AuthSysMenu findById(Long mnId) {
        return this.menuMapper.selectByPrimaryKey(mnId);
    }

    @Override
    public void addMenu(AuthSysMenu menu) {
        Long menuId = primaryKeyUtil.generatePrimaryKey("authsys_menu");
        menu.setId(menuId);
        // 添加一级menu
        this.menuMapper.insert(menu);
        AuthSysMenuMeta meta = menu.getMeta();
        if(meta != null){
            meta.setMnId(menu.getId());
            this.metaMapper.insert(meta);
        }
    }

    @Override
    public void editMenu(AuthSysMenu menu) {
        this.menuMapper.updateByPrimaryKey(menu);
        if(menu.getMeta() != null){
            this.metaMapper.updateByPrimaryKey(menu.getMeta());
        }
    }

    @Override
    public void deleteMenu(Long id) {
        // 获取子菜单
        List<AuthSysMenu> subMenus = this.menuMapper.selectAllByParentId(id);
        if(subMenus != null) {
            for(AuthSysMenu menu:subMenus) {
                // 删除子菜单的meta
                this.metaMapper.deleteByPrimaryKey(menu.getMeta().getId());
                // 删除子菜单
                this.menuMapper.deleteByPrimaryKey(menu.getId());
            }
        }
        // 删除自己的meta
        this.metaMapper.deleteByMnId(id);
        // 删除自己
        this.menuMapper.deleteByPrimaryKey(id);
    }

    @Override
    public AuthSysMenu findParentMenu(Long id) {
        AuthSysMenu self = this.menuMapper.selectByPrimaryKey(id);
        if(self != null){
            return this.menuMapper.selectByPrimaryKey(self.getParentId());
        }
        return null;
    }

    @Override
    public List<AuthSysMenu> findByRoleId(Long rlId) {
        return this.menuMapper.selectByRlId(rlId);
    }
    /**
     * 根据菜单Id查询子节点列表
     */
    List<AuthSysMenu> getChildrenMenus( Long parentId){
        List<AuthSysMenu> childrenMenus = menuMapper.getChildrenMenus(parentId);
        if(childrenMenus != null){
            for (AuthSysMenu authSysMenu : childrenMenus){
                //查询路由的模板
                AuthSysMenuMeta authSysMenuMeta = authSysMenuMetaMapper.selectByMnId(authSysMenu.getId());
                authSysMenu.setMeta(authSysMenuMeta);
                authSysMenu.setChildren(getChildrenMenus(authSysMenu.getId()));
                //查询路由的角色信息
                List<Long> muneRoleList = menuMapper.getMuneRoleList(authSysMenu.getId());
                authSysMenu.setRoles(muneRoleList);
            }
        }
        return childrenMenus;
    }
}
